﻿#include <iostream>
#include <string>
#include <fstream>
#include <memory>
#include <vector>

#include "cJSON.h"
std::string ReadFile(std::string filePath){
    std::ifstream ifs(filePath);
    if(!ifs.is_open()){
        return "";
    }
    std::string line;
    std::string content;
    while (getline(ifs, line)) { // 逐行读取文件内容
        content+=line;
    }

    ifs.close(); // 关闭文件
    return content;
}
struct cJSON_Deleter {
    void operator()(cJSON* json) const {
        cJSON_free(json);
    }
};

// 解析对象
void parse_object1(){
    std::string content = ReadFile("data/object1.txt");
    if(content.empty()){
        return;
    }
    std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_Parse(content.c_str()));
    if(json_data == nullptr){
        std::cout <<"解析失败！";
    }
    cJSON *json_name = cJSON_GetObjectItem(json_data.get(),"name");
    cJSON *json_age = cJSON_GetObjectItem(json_data.get(),"age");
    cJSON *json_hight = cJSON_GetObjectItem(json_data.get(),"hight");
    std::string name = json_name->valuestring;
    int age = json_age->valueint;
    double hight = json_hight->valuedouble;
    std::cout <<"name:"<<name<<std::endl;
    std::cout <<"age:"<<age<<std::endl;
    std::cout <<"hight:"<<hight<<std::endl;
}

void parse_array1(){
    std::string content = ReadFile("data/array1.txt");
    if(content.empty()){
       std::cout<<"读取内容失败";
        return;
    }
    std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_Parse(content.c_str()));
    int len = cJSON_GetArraySize(json_data.get());
    for(int i = 0 ;i < len ; i++){
       cJSON *item = cJSON_GetArrayItem(json_data.get(),i);
        if(item){
           std::cout <<"item:"<<item->valuestring<<std::endl;
        }
     }
}
void parse_array2(){
    std::string content = ReadFile("data/array1.txt");
    if(content.empty()){
        std::cout<<"读取内容失败";
        return;
    }
    std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_Parse(content.c_str()));
    cJSON* item = nullptr;
    cJSON_ArrayForEach(item, json_data.get()) {
        if (cJSON_IsString(item)) {
            std::cout << "item: " << item->valuestring<< std::endl;
        }
    }
}

void parse_object_array(){
    std::string content = ReadFile("data/object_array.txt");
    if(content.empty()){
        std::cout<<"读取内容失败";
        return;
    }
    std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_Parse(content.c_str()));
    cJSON *json_fruit_array = cJSON_GetObjectItem(json_data.get(),"fruit");
    cJSON* item = nullptr;
    cJSON_ArrayForEach(item, json_fruit_array) {
        if (cJSON_IsString(item)) {
            std::cout << "fruit item: " << item->valuestring<< std::endl;
        }
    }
    cJSON *json_unit_array = cJSON_GetObjectItem(json_data.get(),"unit");
    cJSON_ArrayForEach(item, json_unit_array) {
        if (cJSON_IsString(item)) {
            std::cout << "unit item: " << item->valuestring<< std::endl;
        }
    }
}

void parse_array_object(){
    std::string content = ReadFile("data/array_object.txt");
    if(content.empty()){
        std::cout<<"读取内容失败";
        return;
    }
     std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_Parse(content.c_str()));
     // 先获取students的json
     cJSON *json_student_array = cJSON_GetObjectItem(json_data.get(),"students");
     if(cJSON_IsNull( json_student_array)){
         std::cout<<"students 解析失败";
         return ;
     }
     if(cJSON_IsArray(json_student_array)){
         cJSON* item = nullptr;
         cJSON_ArrayForEach(item,json_student_array){
             cJSON* json_item_name = cJSON_GetObjectItem(item,"name");
             cJSON* json_item_age = cJSON_GetObjectItem(item,"age");
             if(json_item_name){
                 std::cout <<"name:"<<json_item_name->valuestring;
             }
             if(json_item_age){
                 std::cout <<",age:"<<json_item_age->valueint;
             }
             std::cout << std::endl;
         }
     }
}
void CreateIntArray(){
    int num[] = {10,4,5,6,1};

    cJSON *json_int_array = cJSON_CreateIntArray(num,sizeof(num)/sizeof(int));
    std::cout << cJSON_Print(json_int_array)<<std::endl;;
    cJSON_Delete(json_int_array );

}
void CreateFloatArray(){
    float num[] = {10.1,4.2,5.3,6.4,1.5};
    cJSON *json_float_array = cJSON_CreateFloatArray(num,sizeof(num)/sizeof(float));
    std::cout << cJSON_Print(json_float_array)<<std::endl;;
    cJSON_Delete(json_float_array );
}
void CreateDoubleArray(){
    double num[] = {10.1,4.2,5.3,6.4,1.5};
    cJSON *json_float_array = cJSON_CreateDoubleArray(num,sizeof(num)/sizeof(double));
    std::cout << cJSON_Print(json_float_array)<<std::endl;;
    cJSON_Delete(json_float_array );
}
void CreateStringArray(){
    const char *string[] = {    "Hello",    "World",    "I",    "Am",    "Programmer"};
    cJSON *json_string_array = cJSON_CreateStringArray(string,sizeof(string)/sizeof(string[0]));
    std::cout << cJSON_Print(json_string_array)<<std::endl;;
    cJSON_Delete(json_string_array);
}
void CreateObejectArray(){
    std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_CreateObject());
    const  char *string[] = {    "Hello",    "World",    "I",    "Am",    "Programmer"};
    cJSON *json_string_array = cJSON_CreateStringArray(string,sizeof(string)/sizeof(string[0]));
    double num[] = {10.1,4.2,5.3,6.4,1.5};
    cJSON *json_float_array = cJSON_CreateDoubleArray(num,sizeof(num)/sizeof (double));
    cJSON_AddItemToObject(json_data.get(),"word",json_string_array );
    cJSON_AddItemToObject(json_data.get(),"number",json_float_array );
    std::cout  <<cJSON_Print(json_data.get())<<std::endl;
}
struct Student{
    Student(std::string name ,int age ,std::string sex){
        this->name = name;
        this->age = age;
        this->sex = sex;
    }
    std::string name;
    int age;
    std::string sex;
};
void CreateArrayObeject(){
    std::unique_ptr<cJSON,cJSON_Deleter> json_data(cJSON_CreateArray());
    std::vector<Student> vstu;
    Student stu1("zhangsan",10,"nan");
    Student stu2("lisi",12,"nan");
    Student stu3("wangwu",13,"nv");
    vstu.push_back(stu1);
    vstu.push_back(stu2);
    vstu.push_back(stu3);
    for(auto stu :vstu){
        cJSON *item = cJSON_CreateObject();
        cJSON_AddItemToObject(item ,"name",cJSON_CreateString(stu.name.c_str()));
        cJSON_AddItemToObject(item ,"age",cJSON_CreateNumber(stu.age));
        cJSON_AddItemToObject(item ,"sex",cJSON_CreateString(stu.sex.c_str()));
        cJSON_AddItemToArray(json_data.get(),item);
    }
    std::cout  <<cJSON_Print(json_data.get())<<std::endl;
}
int main(){
    // parse_object1();
    // parse_array1();
    // parse_array2();
    // parse_object_array();
    // parse_array_object();
//    CreateIntArray();
//    CreateFloatArray();
//    CreateDoubleArray();
//    CreateStringArray();
    // CreateObejectArray();
    CreateArrayObeject();
    return 0;
}
